package com.example.shop.goods.dto;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.goods.enums.ShelfState;
import com.example.shop.pojo.Goods;
import com.example.shop.pojo.GoodsType;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.elasticsearch.annotations.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@Accessors(chain = true)
@Document(indexName = "goods-detail-browse-info",shards = 2,refreshInterval = "30s")
public class GoodsDetailBrowseInfo extends GoodsBrowseInfo implements Serializable {

    /**
     * 商品的简介
     */
    @MultiField(
            mainField = @Field(type = FieldType.Text, analyzer = "ik_max_word",searchAnalyzer = "ik_max_word"),
            otherFields = {
                    @InnerField(suffix = "keyword",type = FieldType.Keyword)
            }
    )
    protected String goodsIntroduction;
    /**
     * 商品的视频介绍
     */
    @Field(type = FieldType.Keyword)
    protected String goodsVideo;
    /**
     * 商品详细图片,比如：价值、效果、保证等
     */
    @Field(type = FieldType.Keyword)
    protected List<String> goodsDetailsPicture;
    /**
     * 该商品的好评数
     */
    protected int favorableCommentNumber;
    /**
     * 该商品的中评数
     */
    protected int mediumCommentNumber;
    /**
     * 该商品的差评数
     */
    protected int badCommentNumber;
    /**
     * 该商品的上架时间
     */
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime shelfTime;
    /**
     * 该商品的上架状态
     */
    protected ShelfState shelfState;
    /**
     * 评价总数：由  好评数+中评数+差评数
     */
    protected int evaluationTotalNum;
    /**
     * 好评率：由  好评数/评价数
     */
    protected float favorableRating;
    /**
     * 该商品的评价
     */
    protected List<EvaluationBrowseInfo> evaluationList;

    public GoodsDetailBrowseInfo(long goodsId){
        this.goodsId = goodsId;
    }

    public GoodsDetailBrowseInfo setGoodsVideo(String goodsVideo) {
        this.goodsVideo = ResponseResult.toRequestPath(goodsVideo);
        return this;
    }

    public GoodsDetailBrowseInfo setGoodsDetailsPicture(List<String> goodsDetailsPicture) {
        ArrayList<String> requestList = new ArrayList<>();
        for(String resource:goodsDetailsPicture){
            requestList.add(ResponseResult.toRequestPath(resource));
        }
        this.goodsDetailsPicture = requestList;
        return this;
    }

    public GoodsDetailBrowseInfo setFavorableCommentNumber(Integer favorableCommentNumber) {
        this.favorableCommentNumber = favorableCommentNumber;
        compute();
        return this;
    }

    public GoodsDetailBrowseInfo setMediumCommentNumber(Integer mediumCommentNumber) {
        this.mediumCommentNumber = mediumCommentNumber;
        compute();
        return this;
    }

    public GoodsDetailBrowseInfo setBadCommentNumber(Integer badCommentNumber) {
        this.badCommentNumber = badCommentNumber;
        compute();
        return this;
    }

    private void compute(){
        evaluationTotalNum =  this.favorableCommentNumber + this.mediumCommentNumber + this.badCommentNumber;
        if(evaluationTotalNum!=0){
            favorableRating = this.favorableCommentNumber/evaluationTotalNum;
        }else{

        }
    }

    @Override
    public String toString() {
        return "GoodsDetailBrowseInfo{" +
                super.toString() +
                "goodsIntroduction='" + goodsIntroduction + '\'' +
                ", goodsVideo='" + goodsVideo + '\'' +
                ", goodsDetailsPicture=" + goodsDetailsPicture +
                ", favorableCommentNumber=" + favorableCommentNumber +
                ", mediumCommentNumber=" + mediumCommentNumber +
                ", badCommentNumber=" + badCommentNumber +
                ", shelfTime=" + shelfTime +
                ", shelfState=" + shelfState +
                ", evaluationTotalNum=" + evaluationTotalNum +
                ", favorableRating=" + favorableRating +
                ", evaluationList=" + evaluationList +
                '}';
    }

    public GoodsDetailBrowseInfo assignment(Goods goods, List<String> descriptionPicture, List<String> detailsPicture, List<GoodsStyle> goodsStyleList,
                                            List<EvaluationBrowseInfo> evaluationList){
        super.assignment(goods,descriptionPicture,goodsStyleList);
        this.setGoodsIntroduction(goods.getGoodsIntroduction())
                .setGoodsVideo(goods.getGoodsVideo())
                .setGoodsDetailsPicture(detailsPicture)
                .setFavorableCommentNumber(goods.getFavorableCommentNumber())
                .setMediumCommentNumber(goods.getMediumCommentNumber())
                .setBadCommentNumber(goods.getBadCommentNumber())
                .setShelfTime(goods.getShelfTime())
                .setShelfState(ShelfState.getShelfState(goods.getShelfState()))
                .setEvaluationList(evaluationList);
        return this;
    }
    public static GoodsDetailBrowseInfo getDetailBrowseInfo(Goods goods,List<String> descriptionPicture,List<String> detailsPicture,List<GoodsStyle> goodsStyleList){
        GoodsDetailBrowseInfo detailBrowseInfo = new GoodsDetailBrowseInfo();
        detailBrowseInfo.setGoodsId(goods.getGoodsId());
        detailBrowseInfo.setMerchantId(goods.getMerchantId());
        detailBrowseInfo.setGoodsType(GoodsType.valueOf(goods.getGoodsType()));
        detailBrowseInfo.setGoodsName(goods.getGoodsName());
        detailBrowseInfo.setGoodsVideo(goods.getGoodsVideo());
        detailBrowseInfo.setGoodsIntroduction(goods.getGoodsIntroduction());
        detailBrowseInfo.setPurchasedTime(goods.getPurchasedTime());

        detailBrowseInfo.setFavorableCommentNumber(goods.getFavorableCommentNumber());
        detailBrowseInfo.setMediumCommentNumber(goods.getMediumCommentNumber());
        detailBrowseInfo.setBadCommentNumber(goods.getBadCommentNumber());

        detailBrowseInfo.setShelfState(ShelfState.getShelfState(goods.getShelfState()));
        detailBrowseInfo.setShelfTime(goods.getShelfTime());



        detailBrowseInfo.setGoodsDescriptionPicture(descriptionPicture);
        detailBrowseInfo.setGoodsDetailsPicture(detailsPicture);
        detailBrowseInfo.setGoodsStyleList(goodsStyleList);
        return detailBrowseInfo;
    }
}
